package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录操作
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
//        通过md5获取到密码加密格式
        String password = employee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
//        通过用户查询获取到Employee对象
        LambdaQueryWrapper<Employee> lqw = Wrappers.lambdaQuery();
        lqw.eq(Employee::getUsername,employee.getUsername());
        lqw.eq(Employee::getStatus,1);
        Employee employeeFromDb = employeeService.getOne(lqw);
//        判断是否有该用户名
        if (employeeFromDb == null){
            return R.error("登录失败,用户名或密码错误");
        }
//        判断密码是否正确
        if (!md5password.equals(employeeFromDb.getPassword())){
            return R.error("登录失败,用户名或密码错误");
        }
//        判断用户是否禁用
        if ("0".equals(employeeFromDb.getStatus())){
            return R.error("用户已被禁用");
        }
        request.getSession().setAttribute("employee",employeeFromDb.getId());
        return R.success(employeeFromDb);
    }

    /**
     * 登出操作
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<Employee> logOut(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success(new Employee());
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee , HttpServletRequest request){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("");
    }

    @GetMapping("/page")
    public R<Page> page(int page ,int pageSize , String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lqw.orderByAsc(Employee::getCreateTime);
        employeeService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee ,HttpServletRequest request){
        employeeService.updateById(employee);
        return R.success("");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
